Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new(metrics): add rules_counters_enabled option #3192

Merged
merged 6 commits into from
May 17, 2024

Conversation

incertum
Copy link
Contributor

@incertum incertum commented May 13, 2024

Intended to replace https://github.com/falcosecurity/falco-exporter when used with Prometheus output

What type of PR is this?

Uncomment one (or more) /kind <> lines:

/kind bug

/kind cleanup

/kind design

/kind documentation

/kind failing-test

/kind feature

/kind release

Any specific area of the project related to this PR?

Uncomment one (or more) /area <> lines:

/area build

/area engine

/area tests

/area proposals

/area CI

What this PR does / why we need it:

new(metrics): add rules_counters_enabled option

Intended to replace https://github.com/falcosecurity/falco-exporter when used with Prometheus output.
CC @leogr and @jasondellaluce (current maintainers of https://github.com/falcosecurity/falco-exporter).

Which issue(s) this PR fixes:

Fixes #3132

Fixes #

Special notes for your reviewer:

Does this PR introduce a user-facing change?:

new(metrics): add `rules_counters_enabled` option

Copy link

This PR may bring feature or behavior changes in the Falco engine and may require the engine version to be bumped.

Please double check userspace/engine/falco_engine_version.h file. See versioning for FALCO_ENGINE_VERSION.

/hold

@incertum
Copy link
Contributor Author

Proposing

/milestone 0.38.0

so that we can start the deprecation cycle for https://github.com/falcosecurity/falco-exporter @leogr .

@poiana poiana added this to the 0.38.0 milestone May 13, 2024
@incertum
Copy link
Contributor Author

@sgaist this PR includes super minor touch ups re variable naming for the Prometheus webserver code.
In general once we start the dev cycle for Falco 0.39.0 we should chat re how to consolidate and simplify the Falco metrics model even more.

@leogr
Copy link
Member

leogr commented May 14, 2024

Proposing

/milestone 0.38.0

so that we can start the deprecation cycle for https://github.com/falcosecurity/falco-exporter @leogr .

If we are able to get the PR reviewed in time, +1 for me. I'll take a look as soon as I can.

/assign
/assign @FedeDP
(as discussed)

auto rule = rules.at(i);
std::string rules_metric_name = "rules." + rule->name;
// Separate processing of rules counter metrics given we add extra tags
auto metric = libs_metrics_collector.new_metric(rules_metric_name.c_str(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of scope for this PR; but since we only instantiate libs_metrics_collector to add metrics to it, that, given the code: https://github.com/falcosecurity/libs/blob/master/userspace/libsinsp/metrics_collector.h#L260
seems like it can be made static, if we made the change in libs (ie: make new_metric static), can we drop the libs_metrics_collector instance?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, I'll create an issue later to track all the post Falco 0.38.0 metrics items.

@@ -86,26 +86,59 @@ std::string falco_metrics::to_text(const falco::app::state& state)
{
prometheus_text += prometheus_metrics_converter.convert_metric_to_text_prometheus("evt_source", "falcosecurity", "falco", {{"evt_source", source}});
}
std::vector<metrics_v2> static_metrics;
static_metrics.push_back(libs_metrics_collector.new_metric("start_ts",
std::vector<metrics_v2> falco_metrics;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to split this method into multiple logical pieces, like:

  • scap_engines_to_text()
  • falco_rules_to_text()
  • agent_info_to_text()

...and so on.
Do you think it is feasible?

Copy link
Contributor Author

@incertum incertum May 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course. Big question is now or wait until a more major cleanup follows post Falco 0.38.0, cc @sgaist?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't have strong opinion; for me, the sooner the better (helps code readability), but we can postpone it without any problem.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @FedeDP was looking into it and it would have made things more ugly. We need a proper new falco metrics model as @sgaist suggested somewhere here #3140 and think more carefully how to split things.

Meanwhile, I added some more comments, is this acceptable @FedeDP 🙃 ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much better, thanks!

userspace/engine/stats_manager.h Show resolved Hide resolved
Comment on lines 67 to 86
std::vector<std::unique_ptr<std::atomic<uint64_t>>> m_by_priority;
std::vector<std::unique_ptr<std::atomic<uint64_t>>> m_by_rule_id;
Copy link
Contributor

@sgaist sgaist May 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Outside of this PR scope but why unique pointers to atomic rather than just atomic ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FedeDP
FedeDP previously approved these changes May 16, 2024
Copy link
Contributor

@FedeDP FedeDP left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/approve

@poiana poiana added the lgtm label May 16, 2024
@poiana
Copy link
Contributor

poiana commented May 16, 2024

LGTM label has been added.

Git tree hash: 66ff78a9dd26a9b03a03f354cd1bedb00612fff1

Comment on lines 169 to 179
for (size_t i = 0; i < rule_stats_manager.get_by_rule_id().size(); i++)
{
auto rule = rules.at(i);
std::string rules_metric_name = "rules." + rule->name;
// Separate processing of rules counter metrics given we add extra tags
auto metric = libs_metrics_collector.new_metric(rules_metric_name.c_str(),
METRICS_V2_RULE_COUNTERS,
METRIC_VALUE_TYPE_U64,
METRIC_VALUE_UNIT_COUNT,
METRIC_VALUE_METRIC_TYPE_MONOTONIC,
rule_stats_manager.get_by_rule_id()[i]->load());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about minimizing the number of calls to get_by_rule_id ?

Suggested change
for (size_t i = 0; i < rule_stats_manager.get_by_rule_id().size(); i++)
{
auto rule = rules.at(i);
std::string rules_metric_name = "rules." + rule->name;
// Separate processing of rules counter metrics given we add extra tags
auto metric = libs_metrics_collector.new_metric(rules_metric_name.c_str(),
METRICS_V2_RULE_COUNTERS,
METRIC_VALUE_TYPE_U64,
METRIC_VALUE_UNIT_COUNT,
METRIC_VALUE_METRIC_TYPE_MONOTONIC,
rule_stats_manager.get_by_rule_id()[i]->load());
auto rules_by_id = rule_stats_manager.get_by_rule_id();
for (size_t i = 0; i < rules_by_id.size(); i++)
{
auto rule = rules.at(i);
std::string rules_metric_name = "rules." + rule->name;
// Separate processing of rules counter metrics given we add extra tags
auto metric = libs_metrics_collector.new_metric(rules_metric_name.c_str(),
METRICS_V2_RULE_COUNTERS,
METRIC_VALUE_TYPE_U64,
METRIC_VALUE_UNIT_COUNT,
METRIC_VALUE_METRIC_TYPE_MONOTONIC,
rules_by_id[i]->load());

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incorporated, thanks.

@poiana
Copy link
Contributor

poiana commented May 16, 2024

@sgaist: changing LGTM is restricted to collaborators

In response to this:

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@poiana poiana removed the lgtm label May 16, 2024
@poiana poiana requested a review from FedeDP May 16, 2024 19:30
Intended to replace https://github.com/falcosecurity/falco-exporter
when used with Prometheus output

Signed-off-by: Melissa Kilby <melissa.kilby.oss@gmail.com>
incertum added 2 commits May 16, 2024 21:15
Signed-off-by: Melissa Kilby <melissa.kilby.oss@gmail.com>
Signed-off-by: Melissa Kilby <melissa.kilby.oss@gmail.com>
@incertum incertum force-pushed the rule-counters-metrics branch from f9fd0a3 to 02c112a Compare May 16, 2024 21:15
Co-authored-by: Samuel Gaist <samuel.gaist@idiap.ch>
Signed-off-by: Melissa Kilby <melissa.kilby.oss@gmail.com>
@incertum incertum force-pushed the rule-counters-metrics branch from 02c112a to ea01751 Compare May 16, 2024 21:36
@incertum
Copy link
Contributor Author

Note that just occurred to me: Let me check in the morning if white spaces in the rules names cause issues for Prometheus. Perhaps let's replace white spaces of the rules names with underscore? Other ideas?

@sgaist
Copy link
Contributor

sgaist commented May 17, 2024

Note that just occurred to me: Let me check in the morning if white spaces in the rules names cause issues for Prometheus. Perhaps let's replace white spaces of the rules names with underscore? Other ideas?

Based on the OpenMetrics text format the metric name should follow these rules:

metricname = metricname-initial-char 0*metricname-char

metricname-char = metricname-initial-char / DIGIT
metricname-initial-char = ALPHA / "_" / ":"

So white spaces would indeed be wrong. As a safety measure, I would recommend turning anything that does not fit the metric name definition into an underscore.

std::string sanitize_metric_name(const std::string& name)
{
std::string sanitized_name = name;
RE2::GlobalReplace(&sanitized_name, "\\s+", "_");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure that only white spaces are the problematic characters that can be found in a metric name ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i read your comment after I pushed lol, repushed just now.

@incertum incertum force-pushed the rule-counters-metrics branch from 0b03b07 to 805b552 Compare May 17, 2024 07:34
@incertum
Copy link
Contributor Author

@sgaist and @FedeDP ok added the proper helper (also for the config and rules sha256 metric names, just in case). I was about to go to sleep yesterday and then that thought came into my head. Good we fix that before merging indeed ...

@incertum incertum force-pushed the rule-counters-metrics branch from 805b552 to ea01751 Compare May 17, 2024 07:38
Signed-off-by: Melissa Kilby <melissa.kilby.oss@gmail.com>
@incertum
Copy link
Contributor Author

incertum commented May 17, 2024

A rule name like Testing rule 2 (CVE-2244) becomes falco.rules.Testing_rule_2_CVE_2244 now.

or for Prometheus:

falcosecurity_falco_rules_Testing_rule_2_CVE_2244_total

Signed-off-by: Melissa Kilby <melissa.kilby.oss@gmail.com>
Copy link
Contributor

@FedeDP FedeDP left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/approve

@poiana poiana added the lgtm label May 17, 2024
@poiana
Copy link
Contributor

poiana commented May 17, 2024

LGTM label has been added.

Git tree hash: 62368cf1074053cda7d7b3356a98a1f9bdb62dd7

@poiana
Copy link
Contributor

poiana commented May 17, 2024

@sgaist: changing LGTM is restricted to collaborators

In response to this:

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@poiana
Copy link
Contributor

poiana commented May 17, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: FedeDP, incertum, LucaGuerra, sgaist

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:
  • OWNERS [FedeDP,LucaGuerra,incertum]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@incertum
Copy link
Contributor Author

/unhold

@poiana poiana merged commit b0f352e into falcosecurity:master May 17, 2024
35 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
6 participants